package cn.message.service.impl;

import cn.itcast.mapper.*;
import cn.itcast.pojo.*;
import cn.message.service.IShowChartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 郭智超
 * @create 2020-04-16 8:31
 */
@Service
@Slf4j
public class ShowChartServiceImpl implements IShowChartService {

    @Resource
    private recycleDetailMapper recycleDetailMapper;

    @Resource
    private orderItemMapper orderItemMapper;

    @Resource
    private orderMapper orderMapper;

    @Resource
    private cn.itcast.mapper.userMapper userMapper;

    @Resource
    private user_addressMapper user_addressMapper;

    /**
     * 获取一天中所有人员占的总货量比例
     * @return
     */
    @Override
    public List<recycleDetail> getPieChart() {
        List<recycleDetail> pieChart = recycleDetailMapper.getBarChart();
        return pieChart;
    }

    /**
     * 一个月中每天的货量变化趋势
     * @return
     */
    @Override
    public List<recycleDetail> getLineChart() {
        List<recycleDetail> lineChart = recycleDetailMapper.getLineChart();
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        for (recycleDetail recycleDetail : lineChart) {
            recycleDetail.setRecycleDate_string(sdf.format(recycleDetail.getRecycleDate()));
        }
        return lineChart;
    }

    @Override
    public List<recycleDetail> getBarChart() {
        List<recycleDetail> barChart = recycleDetailMapper.getBarChart();
        return barChart;
    }

    /**
     * 骑手的货量与金额
     * @return
     */
    @Override
    public List<horsemanWeightAndSum> getHorsemanWeightAndSum() {
        List<horsemanWeightAndSum> horsemanWeightAndSumList = orderMapper.getHorsemanWeightAndSum();
        return horsemanWeightAndSumList;
    }

    /**
     * 系统成交总重量
     * @return
     */
    @Override
    public Double getTotalWeight() {
        Double totalWeight = orderMapper.getTotalWeight();
        return totalWeight;
    }

    /**
     * 系统成交总金额
     * @return
     */
    @Override
    public Double getTotalSum() {
        Double totalSum = orderMapper.getTotalSum();
        return totalSum;
    }

    /**
     * 累计下单次数
     * @return
     */
    @Override
    public int getOrderCount() {
        orderExample orderExample = new orderExample();
        int count = orderMapper.countByExample(orderExample);
        return count;
    }

    /**
     * 累计用户人数
     * @return
     */
    @Override
    public int getUserCount() {
        userExample userExample = new userExample();
        int count = userMapper.countByExample(userExample);
        return count;
    }

    /**
     * 获取5月份每日货量
     * @return
     */
    @Override
    public List<orderItem> getEverydayCommodity() {
        List<orderItem> orderItemList = orderItemMapper.getEverydayCommodity();
        return orderItemList;
    }

    /**
     * 回收重量类型分布
     * @return
     */
    @Override
    public List<orderItem> weightDistribution() {
        List<orderItem> orderItemList = orderItemMapper.getWeightDistribution();
        return orderItemList;
    }

    /**
     * 用户下单次数分布
     * @return
     */
    @Override
    public List<order> orderCountDistribution() {
        List<order> orderList = orderMapper.getOrderCountDistribution();

        return orderList;
    }

    /**
     * 回收区域重量分布
     * @return
     */
    @Override
    public List<order> weightCountyDistribution() {
        List<order> orderList = orderMapper.getWeightCountyDistribution();
        return orderList;
    }

    /**
     * 从用户地址表中查询用户的区地址插入到订单表中的区地址字段
     */
    @Override
    public void insertOrderDistrict() {
        //1.根据订单表中的openid去地址表中查询该用户的区域名称
        orderExample orderExample = new orderExample();
        List<order> orderList = orderMapper.selectByExample(orderExample);
        //用count来记录插入成功的次数，成功一次则加一
        int count = 0;

        //2.把查询出来的区域地址插入到订单表中的区域字段中
        for (order order : orderList) {
            List<user_address> user_addressList = user_addressMapper.getCountryByOpenid(order.getOpenId());
            if(user_addressList.size() != 0){
                cn.itcast.pojo.order order2 = new order();
                order2.setId(order.getId());
                order2.setCounty(user_addressList.get(0).getCounty());
                int i = orderMapper.updateByPrimaryKeySelective(order2);
                if(i == 1){
                    count++;
                }
            }
        }
        log.debug("count："+String.valueOf(count));
    }

    /**
     * 获取日最高货量
     * @return
     */
    @Override
    public double getMaxCommodity() {
        List<orderItem> maxCommodity = orderItemMapper.getMaxCommodity();
        double asDouble = maxCommodity.stream().mapToDouble(orderItem::getFinallyWeight).max().getAsDouble();
        return asDouble;
    }
}
